﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Threading;

namespace IWebApp2
{
    public partial class _Default : System.Web.UI.Page
    {
        #region Variables
        //this varibale will accept the result in a callback method
        private static bool Done = false;
        //call ybProxy in order to have an automatic callback
        private ybProxy yb = new ybProxy();
        #endregion

        protected  void Page_Load(object sender, EventArgs e)
        {
            //there is an irritating tick sound because of timer control
            //find some other workaround(?) here
            
            //means the page is loaded for the first time
            if(!IsPostBack)
            {
                //create worker thread
                ThreadStart simplest = new ThreadStart(RunThread);
                Thread thread1 = new Thread(simplest);
                thread1.IsBackground = true;
                thread1.Start();
            }

        }

        /// <summary>
        /// Thread that invoked method from web service 
        /// </summary>
        private void RunThread()
        {
            yb.IsDoneAsync();
            //create an event handler
            yb.IsDoneCompleted+=new IsDoneCompletedEventHandler(yb_IsDoneCompleted);
        }

        //event handler
        protected void yb_IsDoneCompleted(object sender, IsDoneCompletedEventArgs args)
        {
            //get the result of asynchrounous call to web service
            Done = args.Result;
           
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {

            //check if Done is true, proceed to SignIn.aspx
            if (Done == true) Response.Redirect("SignIn.aspx");
        }

    }
}

